package com.xdk.templatescala.utils;

import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author jhz
 **/
public class BeanCopierUtil {
    private static Lock initLock = new ReentrantLock();
    private static Map<String, BeanCopier> beanCopierMap = new HashMap();

    public BeanCopierUtil() {
    }

    private static BeanCopier initCopier(Class source, Class target,boolean isConverter) {

        BeanCopier beanCopier;
        initLock.lock();
        try {
            BeanCopier find = (BeanCopier)beanCopierMap.get(source.getName() + "_" + target.getName());
            if (find == null) {
                beanCopier = BeanCopier.create(source, target, isConverter);
                beanCopierMap.put(source.getName() + "_" + target.getName(), beanCopier);
                BeanCopier var4 = beanCopier;
                return var4;
            }

            beanCopier = find;
        } catch (Exception var8) {
            throw new RuntimeException(var8);
        } finally {
            initLock.unlock();
        }

        return beanCopier;
    }

    private static BeanCopier getBeanCopier(Class source, Class target,boolean isConverter) {
        BeanCopier beanCopier = (BeanCopier)beanCopierMap.get(source.getClass().getName() + "_" + target.getName());
        return beanCopier != null ? beanCopier : initCopier(source, target,isConverter);
    }

    public static <T> T convert(Object source, Class<T> targetClass,Converter converter) {
        if (source == null) {
            return null;
        } else {
            boolean isConverter=converter!=null;
            BeanCopier beanCopier = getBeanCopier(source.getClass(), targetClass,isConverter);

            try {
                T target = targetClass.newInstance();
                beanCopier.copy(source, target, converter);
                return target;
            } catch (Exception var4) {
                var4.printStackTrace();
                throw new RuntimeException("对象拷贝失败" + source + "_" + targetClass);
            }
        }
    }

    public static <E> List<E> convert(List source, Class<E> targetClass,Converter converter) {
        if (source == null) {
            return null;
        } else {
            try {
                if (source.isEmpty()) {
                    return (List)source.getClass().newInstance();
                } else {
                    List result = (List)source.getClass().newInstance();
                    Iterator var3 = source.iterator();

                    while(var3.hasNext()) {
                        Object each = var3.next();
                        result.add(convert(each,targetClass,converter));
                    }

                    return result;
                }
            } catch (Exception var5) {
                throw new RuntimeException("对象拷贝失败" + source + "_" + targetClass);
            }
        }
    }


}
